package com.og.managedbean;

import java.io.Serializable;
import java.util.List;

import javax.enterprise.context.SessionScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;

import com.og.backingbean.Company;
import com.og.backingbean.Person;
import com.og.dao.CRUDEntityDAO;

/**
 * @author Renaud DIANA
 *
 */
@Named
@SessionScoped
public class PersonBean implements Serializable {

	private static final long serialVersionUID = 1L;
	
	/* Dependency injection */
	// Model
    @Inject
    CRUDEntityDAO<Person> personDAO;
    @Inject
    private CRUDEntityDAO<Company> companyDAO;
    // BackingBean    
    @Inject
    Person personView;
    @Inject
    Person personNew;
    @Inject
    Person personUpdate;
    @Inject
    private Company company;
    @Inject
    private ListBean list;
    
    /* -------------------- */
    
	/**
	 * Default constructor
	 */
	public PersonBean() {
		// TODO Auto-generated constructor stub
	}
	
	public String create() {
		
		// Add FK
		personNew.setCompany(company);
		
		// Set List						
		List<Person> listPersons = company.getListPersons();
		listPersons.add(personNew);
		company.setListPersons(listPersons);
		
		// Call model company -> to update list
		companyDAO.updateEntity(company);
		
		// Generate new list
		list.findAllPerson();
		list.findAllCompany();
		
		// Clear form
		personNew = new Person();
						
		// Add message
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Create person Successful"));  
		
		// Navigation case
		return "";
		//return "success";
	}
	
	public String update() {
		
		// Call model				
		personDAO.updateEntity(personUpdate);
		
		// Generate new list
		list.findAllPerson();
		
		// Toggle view
		personView = personUpdate;
						
		// Add message
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Update person Successful"));  
		
		// Navigation case
		return "";
		//return "success";
	}
	
	public String delete() {
				
		// Set List						
		company.getListPersons().remove(personView);
						
		// Call model			
		// person -> to delete the entity
		personDAO.deleteEntity(personView);
		// company -> to update list
		companyDAO.updateEntity(company);
		
		// Generate new list
		list.findAllPerson();
		list.findAllCompany();
						
		// Clear form
		personView = new Person();
				
		// Add message
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Delete person Successful"));  
		
		// Navigation case
		return "";
		//return "success";
	}
	
	public String findOne() {
		
		// Call model				
		personView = personDAO.findOneEntity(personView, personView.getIdPerson());
		
		// Add message
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Find person Successful"));  
		
		// Navigation case
		return "";
		//return "success";
	}
	
	public String toggleUpdate() {
		
		personUpdate = personView;
		
		return "";
	}

	// Getters and Setters
			
	public Person getPersonView() {
		return personView;
	}

	public void setPersonView(Person personView) {
		this.personView = personView;
	}

	public Person getPersonNew() {
		return personNew;
	}

	public void setPersonNew(Person personNew) {
		this.personNew = personNew;
	}

	public Person getPersonUpdate() {
		return personUpdate;
	}

	public void setPersonUpdate(Person personUpdate) {
		this.personUpdate = personUpdate;
	}

	public Company getCompany() {
		return company;
	}

	public void setCompany(Company company) {
		this.company = company;
	}
			
}
